<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hclick('',false)">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" default-expand-all>
          <template v-slot="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hclick(data.id,false)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hclick(data.id,true)">修改部门</el-dropdown-item>
                        <el-dropdown-item v-if="!data.children.length" @click.native="hdel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>

        <el-dialog
          :title="isEdit?'修改部门':'添加子部门'"
          :visible.sync="showDialog"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <!-- <deptDialog :id="curId" ref="addorEdit" :is-edit="isEdit" @success="hsuccess" /> -->
          <deptDialog v-if="showDialog" :id="curId" ref="addorEdit" :origin-list="originList" :is-edit="isEdit" @close="hclose" @success="hsuccess" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartmentById } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import deptDialog from '@/views/departments/deptDialog'
export default {
  components: {
    deptDialog
  },
  data() {
    return {
      list: [],
      showDialog: false,
      curId: '',
      isEdit: '',
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      try {
        const res = await getDepartments()
        res.data.depts.shift()
        this.originList = res.data.depts.map(item => {
          return {
            id: item.id, code: item.code, name: item.name, pid: item.pid
          }
        })
        this.list = tranListToTreeData(res.data.depts)
      } catch (e) {
        console.log('获取组织架构失败，原因：', e)
      }
    },
    hclick(id, isEdit) {
      this.showDialog = true
      this.curId = id
      this.isEdit = isEdit
      // if (!this.isEdit) return
      //   await this.$nextTick()
      //   this.$refs.addorEdit.loadDepartment()
    },
    hsuccess() {
      this.loadDepartments()
      this.showDialog = false
    },
    hdel(id) {
      this.$confirm('确定要删除吗', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          const res = await delDepartmentById(id)
          this.$message.success(res.message)
          this.loadDepartments()
        } catch (e) {
          this.$message.error(e.message)
        }
      }).catch(e => {})
    },
    hclose() {
      this.showDialog = false
    }
  }
}
</script>

